package com.gzdh.tang.modu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gzdh.tang.comm.vo.Result;
import com.gzdh.tang.modu.entity.Addr;
import com.gzdh.tang.modu.entity.TangDevice;
import com.gzdh.tang.modu.entity.TangReportTime;
import com.gzdh.tang.modu.mapper.TangReportTimeMapper;
import com.gzdh.tang.modu.model.PrintReportModel;
import com.gzdh.tang.modu.model.ReportModel;
import com.gzdh.tang.modu.service.TangAddrService;
import com.gzdh.tang.modu.service.TangDeviceService;
import com.gzdh.tang.modu.service.TangOrderService;
import com.gzdh.tang.modu.service.TangReportTimeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@RestController
@RequestMapping(value = "/tang/report")
public class TangReportTimeController {

    @Autowired
    TangReportTimeService tangReportTimeService;

    @Autowired
    TangOrderService tangOrderService;

    @Autowired
    TangAddrService addrService;

    @Autowired
    TangDeviceService tangDeviceService;

    @Autowired
    TangReportTimeMapper tangReportTimeMapper;

    @GetMapping("getTime")
    public Result<Object> getTime(@RequestParam(name = "operaId", required = false) String operaId) {
        Result<Object> result = new Result<>();
//        List<TangReportTime> tangReportList = tangReportTimeService.list();
        List<TangReportTime> tangReportList = new ArrayList<>();
        TangDevice tangDevice = tangDeviceService.getOne(new QueryWrapper<TangDevice>().lambda().eq(TangDevice::getPosnum, operaId));
        if (operaId == null || "".equals(operaId) || tangDevice == null) {
            tangReportList = tangReportTimeService.list();
        } else {
            tangReportList = tangReportTimeService.list(new QueryWrapper<TangReportTime>().lambda().eq(TangReportTime::getAddr, tangDevice.getAddr()));
        }
        result.setResult(tangReportList);
        return result;
    }

    @PostMapping("getReport")
    public Result<Object> getReport(@RequestParam("timeId") String timeId,
                                    @RequestParam("date") String date,
                                    @RequestParam("addr") int addr,
                                    @RequestParam(name = "posnum", required = false) String posnum) {
        Result<Object> result = new Result<>();
        TangReportTime tangReportTime = tangReportTimeService.getById(timeId);
        if (tangReportTime == null) {
            return Result.error(500, "当前时间段不存在");
        }
        String startTime = date + " " + tangReportTime.getStartTime();
        String endTime = date + " " + tangReportTime.getEndTime();
        List<ReportModel> reportModelList = tangOrderService.getReportModel(addr, startTime, endTime, posnum);
        Addr addr1 = addrService.getOne(new QueryWrapper<Addr>().lambda().eq(Addr::getAddrNo, addr));
        PrintReportModel printReportModel = new PrintReportModel();
        printReportModel.setAddrName(addr1.getAddrName());
        printReportModel.setTimeName(tangReportTime.getName());
        printReportModel.setPrintTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        printReportModel.setReportTime(tangReportTime.getStartTime() + "-" + tangReportTime.getEndTime());
        printReportModel.setReportDate(date);
        printReportModel.setReportModelList(reportModelList);
        result.setResult(printReportModel);
        return result;
    }

    @GetMapping("jiesuan")
    public Result<Object> jiesuan() {
        Result<Object> result = new Result<>();
        String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String startTime = dateStr + " 00:00";
        String endTime = dateStr + " 23:59:59";
        tangReportTimeMapper.updateReportFo(startTime, endTime);
        return result;
    }

    @GetMapping("checkJiesuan")
    public Result<Object> checkJiesuan() {
        Result<Object> result = new Result<>();
        String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String startTime = dateStr + " 00:00";
        String endTime = dateStr + " 23:59:59";
        int count = tangReportTimeMapper.countReportState(startTime, endTime);
        if (count > 0) {
            return Result.error(100, "正在结算#101");
        }
        count = tangReportTimeMapper.countReportState2(startTime, endTime);
        if (count > 0) {
            return Result.error(100, "正在结算#102");
        }
        return result;
    }

    @GetMapping("getScjReport")
    public Result<Object> getScjPort() {
        Result<Object> result = new Result<>();
        String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String startTime = dateStr + " 00:00:00";
        String endTime = dateStr + " 23:59:59";
        Map<String, Object> map = new HashMap<>();
        map.put("weixin", tangReportTimeMapper.sumWeixinReport(startTime, endTime));
        map.put("ali", tangReportTimeMapper.sumAliReport(startTime, endTime));
        result.setResult(map);
        return result;
    }


}
